import pygame
import sys
from ship import Ship
from spider_fleet import SpiderFleet
from btn_ready import BtnReady
from score_board import ScoreBoard
from music import Music


class GameFrame:
    def __init__(self):
        self.dd = (1024, 768)
        self.title = "girl & spider"
        self.bg_color = (230, 230, 230)
        pygame.init()
        self.screen = pygame.display.set_mode(self.dd)
        pygame.display.set_caption(self.title)
        self.music = Music()

        self.game_flag = False
        self.btn_ready = None
        self.ship = None
        self.spider_fleet = None
        self.score_board = ScoreBoard(None, None, self.screen)
        self.bg = None

    def set_bg(self, src="rc/pic/bg_forest.png"):
        if self.bg is None:
            self.bg = pygame.image.load(src).convert()
        self.screen.blit(self.bg, self.bg.get_rect())

    def check_game(self):
        while True:
            self.set_bg()
            if self.game_flag:
                self.run_game()
            else:
                self.ready_game()
            pygame.display.flip()

    def ready_game(self):
        assert self.score_board is not None
        self.score_board.blit_scoreboard()
        if self.btn_ready is None:
            self.btn_ready = BtnReady(self.screen)
        self.btn_ready.blit_btn()
        for ev in pygame.event.get():
            if ev.type == pygame.QUIT:
                sys.exit()
            elif ev.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                if self.btn_ready.img_rect.collidepoint(mouse_x, mouse_y):
                    self.game_flag = True
                    self.btn_ready = None
                    pygame.mouse.set_visible(False)
                    pygame.event.clear()
                    self.music.play_bgm()

    def run_game(self):
        if self.ship is None:
            self.ship = Ship(self.screen, "rc/pic/ic_actor.png", 1.5)
        if self.spider_fleet is None:
            self.spider_fleet = SpiderFleet(self.screen)
            self.score_board = ScoreBoard(self.ship.bullets, self.spider_fleet, self.screen)
        assert self.ship is not None
        assert self.spider_fleet is not None
        assert self.score_board is not None
        self.ship.moving_update()
        self.spider_fleet.moving_update()
        self.score_board.update_score()
        self.score_board.blit_scoreboard(False)
        for ev in pygame.event.get():
            if ev.type == pygame.QUIT:
                sys.exit()
            elif ev.type == pygame.USEREVENT + 1:
                self.spider_fleet.add_new_spider()
            elif ev.type == pygame.USEREVENT + 2:
                self.score_board.update_timer()
            self.ship.check_ev(ev)
        if pygame.sprite.spritecollide(self.ship, self.spider_fleet, True):
            pygame.mouse.set_visible(True)
            self.game_flag = False
            self.ship = None
            self.spider_fleet = None
            pygame.event.clear()
            self.music.play_mover()
